#include "chooselevelscence.h"
#include<QMenuBar>
#include"mainscence.h"
#include<QPainter>
#include"mypushbutton.h"
#include<QDebug>
#include<QTimer>
#include<QLabel>
#include<QSound>  //Coin flips // QT       += core gui multimedia

ChooseLevelScence::ChooseLevelScence(QWidget *parent) : QMainWindow(parent)
{
     setFixedSize(320,590);
     this->setWindowIcon(QPixmap(":/res/Coin0001.png"));
     this->setWindowTitle("ChooseLevelScence");
     //create menubar
     QMenuBar*barChooseScence=menuBar();
     setMenuBar(barChooseScence);

     QMenu*MainMenu=barChooseScence->addMenu("菜单");

     QAction*createAction=MainMenu->addAction("新建");
     QAction*openAction=MainMenu->addAction("打开");
     QAction*storyAction=MainMenu->addAction("存储");
     createAction->setVisible(true);
     openAction->setVisible(true);
     storyAction->setVisible(true);
     QAction*backAction=MainMenu->addAction("返回");
     QAction*quitAction=MainMenu->addAction("退出");

     connect(backAction,&QAction::triggered,[=](){
         this->hide();
         MainScence().show();
     });

     connect(quitAction,&QAction::triggered,[=](){
         this->close();
     });


     MyPushButton*backBtn=new MyPushButton(":/res/BackButton.png",":/res/BackButtonSelected.png");
     backBtn->setParent(this);
     backBtn->move(this->width()-backBtn->width(),this->height()-backBtn->height());

     //选择关卡按钮
     //声
      QSound*chooseSound=new QSound(":/res/TapButtonSound.wav",this);
       QSound*backSound=new QSound(":/res/BackButtonSound.wav",this);


     connect(backBtn,&MyPushButton::clicked,[=](){

         qDebug()<<"Press rebackButton";
          backSound->play();
         //告诉主场景，返回按钮
         QTimer::singleShot(500,this,[=](){
             emit this->chooseScenceBack();
         });

     });

     //创建选择关卡按钮
     for(int i=0;i<20;i++)
     {
         MyPushButton*menuBtn=new MyPushButton(":/res/LevelIcon.png");
         menuBtn->setParent(this);
         menuBtn->move(25+i%4*70,130+i/4*70);

         //监听
         connect(menuBtn,&MyPushButton::clicked,[=](){


             chooseSound->play();

             QString str=QString("您选择的是第%1关").arg(i+1);
             qDebug()<<str;

             this->hide();
             play=new PlayScence(i+1);
             //设置游戏场景的初始位置
             play->setGeometry(this->geometry());
             play->show();
             connect(play,&PlayScence::chooseScenceBack,[=](){
                 this->setGeometry(play->geometry());
                 this->show();
                 delete play;
                 play=NULL;
             });

         });

         QLabel*label=new QLabel;
         label->setParent(this);
         label->setFixedSize(menuBtn->width(),menuBtn->height());
         label->setText(QString::number(i+1));
         label->move(25+i%4*70,130+i/4*70);
         label->setAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
         label->setAttribute(Qt::WA_TransparentForMouseEvents);



     }


}





void ChooseLevelScence::paintEvent(QPaintEvent*)
{
     QPainter painter(this);
     QPixmap pix;
     pix.load(":/res/OtherSceneBg.png");
     painter.drawPixmap(0,0,this->width(),this->height(),pix);

     pix.load(":/res/Title.png");
     painter.drawPixmap((this->width() - pix.width())*0.5,30,pix.width(),pix.height(),pix);
}

































